<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/16 0016
 * Time: 11:12
 */

namespace Wx\Controller;
use Think\Controller;

class ReceiveController extends CommonController
{
    //进入验证核销权限
    public function Index(){
        //dump($u_id);
        $order_sn = I("sn")?I("get.sn"):I("post.sn");//spellgroup_order表订单号
        $judge = $this->judge($order_sn);
        //是否有核销权限
        if($judge){
            $this->redirect("Receive/cancel",['sn'=>$order_sn]);
        }else{
            $this->error("无操作权限","http://www.xiangtws.com/index.php/Wx/User/stores.html");
        }
    }//end fun index()

    //权限判断方法
    public function judge($order_sn){
        $u_id = session("u_id");//登录用户id
        //获取店铺店主id和店铺id
        $order_saler = M("shop_spellgroup_order")
            ->alias("`order`")
            ->join(C('DB_PREFIX')."shop_spellgroup_copy copy ON `order`.c_id = copy.id","LEFT")
            ->join(C('DB_PREFIX')."shop_store store ON copy.c_id = store.c_id","LEFT")
            ->where("`order`.order_sn = '{$order_sn}'")
            ->field("store.id,store.u_id")
            ->find();

        if ($order_saler){
//            dump($order_saler);
            //登录用户是否为该订单店主
            $u_id_str = array();
            $u_id_str[0]=$order_saler['u_id'];
            //获取店铺店员id
            $clerk = M("shop_saler")->where("storeid = {$order_saler['id']}")->field('u_id')->select();
            if($clerk){
                foreach ($clerk as $k => $v){
                    $u_id_str[$k+1]=$v['u_id'];
                }
            }
            $u_id_str[] = 1391;
            if(in_array($u_id,$u_id_str)){
                return true;
            }else{
                return false;
            }
        }//end if $order_saler
    }//end fun judge()

    //核销界面
    public function cancel(){
        $order_sn = I("sn")?I("get.sn"):I("post.sn");//spellgroup_order表订单号

        //获取核销信息
        $order_info = M("shop_spellgroup_order")
            ->alias("`order`")
            ->join(C('DB_PREFIX')."shop_spellgroup_copy copy ON `order`.c_id = copy.id","LEFT")
            ->join(C('DB_PREFIX')."shop_spellgroup `group` ON copy.sg_id = `group`.id","LEFT")
            ->where("`order`.order_sn = '{$order_sn}'")
            ->field("`order`.id,`order`.num,`order`.total_fee,`order`.card_fee,`order`.state,`group`.h_imgs,`group`.title,`order`.receive,`order`.tel,`order`.realname,`group`.unit,`group`.u_num,`group`.actual_price,`group`.type,`group`.id as g_id")
            ->find();

        if (IS_POST){
//            $this->ajaxReturn($_POST,"JSON");return;

            $oid = $_POST['oid'];//spellgroup_order表订单id

            $update_order = M("shop_spellgroup_order")->where("id = {$oid}")->save(['state'=>2]);

            if ($update_order){
//            if (true){
                // 检测该社区是否核销完
                $c_id = M('shop_spellgroup_order')->where("id = {$oid}")->getField('c_id');//副本id
                $com_num = M('shop_spellgroup_copy')->where("id = {$c_id}")->getField('num');//总订单份数
                $receive_num = M('shop_spellgroup_order')->where("c_id = {$c_id} AND state in(2,7)")->getField('sum(num)');//已核销订单份数
                if($com_num == $receive_num){
                    M('shop_spellgroup_copy')->where("id = {$c_id}")->save(['state'=>6]);//副本结束
                }

                //给门店打款
                if($_POST['type'] == 0){
                    //农户订单
                    $price_range = M("spellgroup_price_range")->where("sg_id = ".$_POST['g_id']." AND status = 1")->select();//批发价
                    foreach($price_range as $v){
                        if ($com_num >= $v['start'] && $com_num <= $v['end']){
                            $range = $v['price'];
                        }//end if
                    }//end foreach
                    $getprice = ($_POST['actual_price']-$range-($_POST['actual_price']*5/100))*$_POST['num'];
                }else{
                    //门店订单
                    $getprice = $_POST['total_fee']+$_POST['card_fee'];
                }//end if
                if($getprice > 0){
                    $copy_c_id = M('shop_spellgroup_copy')->where("id = {$c_id}")->getField('c_id');//副本小区id
                    $s_u_id = M("shop_store")->where("c_id = {$copy_c_id}")->getField('u_id');//店铺老板id
                    $getprice = sprintf("%.2f", $getprice);
                    $addcredit = M("member")->where("id = {$s_u_id}")->setInc('credit3',$getprice);
                    if ($addcredit){
                        M('shop_spellgroup_order')->where("id = {$oid}")->save(['canceltime'=>time()]);//副本id
                        if($_POST['type'] == 0){
                            $changeCredit = changeCredit($s_u_id,'乡村拼团核销打款','credit3','+',$getprice,'2');//余额变动记录
                            $sendVoice = sendVoice($s_u_id,'乡村拼团核销收益￥'.$getprice);//推送系统消息
                        }else{
                            $changeCredit = changeCredit($s_u_id,'门店拼团核销打款','credit3','+',$getprice,'1');//余额变动记录
                            $sendVoice = sendVoice($s_u_id,'门店拼团核销收益￥'.$getprice);//推送系统消息
                        }//end if

                        if($changeCredit && $sendVoice){
                            $this->ajaxReturn(['state'=>0,'val'=>'核销成功'],"JSON");
                        }
                    }//end if

                }//end if
            }else{
                $this->ajaxReturn(['state'=>1,'val'=>'核销失败'],"JSON");
            }
        }else{
            $this->assign($order_info);
            $this->display('cancel');
        }//end if
//        dump($order_info);
    }//end fun cancel()

    public function pickup(){
        if (IS_POST){
            $receive = I("post.receive");
            $order = M("shop_spellgroup_order")->where("receive = {$receive} AND state = 6")->field("order_sn,id")->find();//获取订单编号和id
            if($order){
                $judge = $this->judge($order['order_sn']);//权限检测
                if($judge){
//                    $update_order = M("shop_spellgroup_order")->where("id = {$order['id']}")->save(['state'=>2]);
//                    if ($update_order){
////                    if (true){
//                        // 检测该社区是否核销完
//                        $c_id = M('shop_spellgroup_order')->where("id = {$order['id']}")->getField('c_id');//副本id
//                        $com_num = M('shop_spellgroup_copy')->where("id = {$c_id}")->getField('num');//总订单份数
//                        $receive_num = M('shop_spellgroup_order')->where("c_id = {$c_id} AND state = 2")->getField('sum(num)');//已核销订单份数
//                        if($com_num == $receive_num){
//                            M('shop_spellgroup_copy')->where("id = {$c_id}")->save(['state'=>6]);//副本结束
//                        }
//
//                        //给门店打款
//
//                        //获取核销信息
//                        $order_info = M("shop_spellgroup_order")
//                            ->alias("`order`")
//                            ->join(C('DB_PREFIX')."shop_spellgroup_copy copy ON `order`.c_id = copy.id","LEFT")
//                            ->join(C('DB_PREFIX')."shop_spellgroup `group` ON copy.sg_id = `group`.id","LEFT")
//                            ->where("`order`.order_sn = '{$order['order_sn']}'")
//                            ->field("`order`.num,`order`.total_fee,`order`.card_fee,`group`.actual_price,`group`.type_id,`group`.id as g_id")
//                            ->find();
//
//                        if($order_info['type_id'] == 0){
//                            //农户订单
//                            $price_range = M("spellgroup_price_range")->where("sg_id = ".$order_info['g_id']." AND status = 1")->select();//批发价
//                            foreach($price_range as $v){
//                                if ($com_num >= $v['start'] && $com_num <= $v['end']){
//                                    $range = $v['price'];
//                                }//end if
//                            }//end foreach
//                            $getprice = ($order_info['actual_price']-$range-($order_info['actual_price']*5/100))*$order_info['num'];
//                        }else{
//                            //门店订单
//                            $getprice = $order_info['total_fee']+$order_info['card_fee'];
//                        }//end if
//                        if($getprice > 0){
//                            $copy_c_id = M('shop_spellgroup_copy')->where("id = {$c_id}")->getField('c_id');//副本小区id
//                            $s_u_id = M("shop_store")->where("c_id = {$copy_c_id}")->getField('u_id');//店铺老板id
//                            $getprice = sprintf("%.2f", $getprice);
//                            M("member")->where("id = {$s_u_id}")->setInc('credit3',$getprice);
//                            if($order_info['type_id'] == 0){
//                                changeCredit($s_u_id,'拼团核销打款','credit3','+',$getprice,'2');//余额变动记录
//                                sendVoice($s_u_id,'乡村拼团核销收益￥'.$getprice);//推送系统消息
//                            }else{
//                                changeCredit($s_u_id,'拼团核销打款','credit3','+',$getprice,'1');//余额变动记录
//                                sendVoice($s_u_id,'门店拼团核销收益￥'.$getprice);//推送系统消息
//                            }//end if
//                        }//end if
////                        $this->AjaxReturn($getprice,"JSON");
                    $this->ajaxReturn(['state'=>0,'order_sn'=>$order['order_sn']],"JSON");
//                    }else{
//                        $this->AjaxReturn('核销失败',"JSON");
//                    }
                }else{
//                    $this->AjaxReturn($order,"JSON");
                    $this->ajaxReturn(['state'=>1,'val'=>'无操作权限'],"JSON");
                }
            }else{
                $this->ajaxReturn(['state'=>1,'val'=>'该订单已核销或不存在'],"JSON");
            }//end if
        }else{
            $this->display("cancel02");
        }

    }//end fun pickup()
}//end class